/**
 * 微信公众平台开发模式(JAVA) SDK
 * (c) 2012-2014 ____′↘夏悸 <wmails@126.cn>, MIT Licensed
 * http://www.jeasyuicn.com/wechat
 */
package com.gson.oauth;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gson.bean.UserInfo;
import com.gson.util.HttpKit;
import com.gson.util.Status;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 用户操作接口
 * @author ____′↘夏悸
 */
public class User {

	private static final String USER_INFO_URI = "https://api.weixin.qq.com/cgi-bin/user/info";
	private static final String USER_GET_URI = "https://api.weixin.qq.com/cgi-bin/user/get";
	private static final String USER_INFO_BATCH_URI ="https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=";

	/**
	 * 拉取用户信息
	 * @param accessToken
	 * @param openid
	 * @return
	 * @throws java.io.IOException
	 * @throws java.security.NoSuchProviderException
	 * @throws java.security.NoSuchAlgorithmException
	 * @throws java.security.KeyManagementException
	 */
	public static UserInfo getUserInfo(String accessToken, String openid) throws Exception {
		Map<String, String> params = new HashMap<String, String>();
		params.put("access_token", accessToken);
		params.put("openid", openid);
		params.put("lang","zh_CN");
		String  jsonStr = HttpKit.get(USER_INFO_URI, params);
		if(StringUtils.isNotEmpty(jsonStr)){
			JSONObject obj = JSONObject.parseObject(jsonStr);
			if(obj.get("errcode") != null){
				throw new Exception(obj.getString("errmsg"));
			}
			UserInfo user = JSONObject.toJavaObject(obj, UserInfo.class);
			return user;
		}
		return null;
	}

	/**
	 * 批量获取用户信息
	 * @param accessToken
	 * @param openIds
	 * @return
	 */
	public static List<UserInfo> batchGetuserInfo(String accessToken,List openIds){
		JSONArray user_list = new JSONArray();
		JSONObject object;
		List<UserInfo> userInfos=new ArrayList<UserInfo>();
		int i=0;
		for(Object openId:openIds){
			object = new JSONObject();
			object.put("openid",openId);
			user_list.add(object);
			i++;
			if(i%100==0||i==openIds.size()-1){
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("user_list", user_list);
				String post = HttpKit.post(USER_INFO_BATCH_URI + accessToken, JSON.toJSONString(params));
				JSONObject parse = JSONObject.parseObject(post);
				List<UserInfo> userInfo100 = JSON.parseArray(parse.getString("user_info_list"), UserInfo.class);
				userInfos.addAll(userInfo100);
				user_list.clear();
			}
		}
		return userInfos;
	}
	
	/**
	 * 获取帐号的关注者列表
	 * @param accessToken
	 * @param next_openid
	 * @return {"total":2,"count":2,"data":{"openid":["","OPENID1","OPENID2"]},"next_openid":"NEXT_OPENID"}
	 */
	public static JSONObject getFollwersList(String accessToken, String next_openid) throws Exception{
		Map<String, String> params = new HashMap<String, String>();
		params.put("access_token", accessToken);
		params.put("next_openid", next_openid);
		String  jsonStr = HttpKit.get(USER_GET_URI, params);
		if(StringUtils.isNotEmpty(jsonStr)){
			JSONObject obj = JSONObject.parseObject(jsonStr);
			if(obj.get("errcode") != null){
				throw new Exception(Status.is(obj.getInteger("errcode")));
			}
			return obj;
		}
		return null;
	}
	
}